package com.tongren.framework.utils;

import android.annotation.SuppressLint;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author lvjian
 * @date 2015-9-28下午2:17:27
 * @description 时间工厂
 */
@SuppressLint("SimpleDateFormat")
public class DateHelper {
    /**
     * @param times
     * @description 时间戳转换为标准日期
     * @date 2015-9-28下午2:18:01
     */
    public static String getDate(String times) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date(Long.parseLong(times)));
    }

    public static String getDate(Long times) {
        if (times == null || times.longValue() == 0l) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(times);
    }

    public static String getDate(String times, String format) {
        if (times == null || times.equals("") || times.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty())
            format = "yyyy-MM-dd HH:mm";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.parseLong(times)));
    }

    public static String getDate(Long times, String format) {
        if (times == null || times.longValue() == 0l) {
            return "";
        }
        if (format == null || format.isEmpty())
            format = "yyyy-MM-dd HH:mm";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(times);
    }

    /**
     * 将符合默认格式的字符串转换成Date
     *
     * @param dateValue 样例:2012-03-29 14:32:23
     * @return
     * @throws ParseException
     */
    public static Date parse(String dateValue, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern(format);
        return sdf.parse(dateValue);
    }

    /**
     * 日期格式字符串转换成时间戳
     *
     * @param date   字符串日期
     * @param format 如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String date2TimeStamp(String date_str, String format) {
        try {
            return String.valueOf(parse(date_str, format).getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds   精确到秒的字符串
     * @param formatStr
     * @return
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty())
            format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.applyPattern(format);
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(Long.parseLong(seconds));
        return sdf.format(cal.getTime());
    }

    /**
     * @param @param  seconds
     * @param @param  format
     * @param @return
     * @return String
     * @throws
     * @Title: timeStamp2Date
     * @Description: 时间戳转换成日期格式字符串
     */
    public static String timeStamp2Date(Long seconds, String format) {
        if (seconds == null || seconds.longValue() == 0l) {
            return "";
        }
        if (format == null || format.isEmpty())
            format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.applyPattern(format);
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(seconds);
        return sdf.format(cal.getTime());
    }


    /**
     * 格式化时间
     *
     * @param dateStr
     * @param formatStr
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String StringToDate(String dateStr, String formatStr) {
        DateFormat sdf = new SimpleDateFormat(formatStr);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String str = sdf.format(date);
        return str;
    }

    /**
     * @param datetime
     * @Title: getYear
     * @Description: 根据传入毫秒数获取年
     * @return: int
     */
    public static int getYear(Long datetime) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(datetime);
        return cal.get(Calendar.YEAR);
    }

    /**
     * @param datetime
     * @Title: getMonth
     * @Description: 根据传入毫秒数获取月
     * @return: int
     */
    public static int getMonth(Long datetime) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(datetime);
        return cal.get(Calendar.MONTH) + 1;
    }

    /**
     * @param @param  datetime
     * @param @return
     * @return int
     * @throws
     * @Title: getDay
     * @Description: 根据传入毫秒数获取天
     */
    public static int getDay(Long datetime) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(datetime);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * @param @param  datetime
     * @param @return
     * @return String
     * @throws
     * @Title: getMonthAndDay
     * @Description: 根据传入毫秒数获取月日 例如12-15
     */
    public static String getMonthAndDay(Long datetime) {
        if (datetime == null || datetime.longValue() == 0l) {
            return "";
        }
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(datetime);
        int m = cal.get(Calendar.MONTH) + 1;
        int d = cal.get(Calendar.DAY_OF_MONTH);
        return m + "-" + d;
    }

    /**
     * @param @param  times
     * @param @return 设定文件
     * @return String    返回类型
     * @throws
     * @Title: getCurrentTime
     * @Description: 获取当前系统时间字符串
     */
    public static String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(new Date());
    }

    /**
     * 返回指定时间加上num天后的时间
     *
     * @param date
     * @param num
     * @return
     */
    public static Long add(Long date, int num) {
        Calendar a = Calendar.getInstance();
        a.setTimeInMillis(date);
        a.add(Calendar.DAY_OF_MONTH, num);
        return a.getTime().getTime();
    }

    /**
     * 返回指定时间加上num天后的时间
     *
     * @param date
     * @param num
     * @return
     */
    public static String add(String date, int num, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Calendar a = Calendar.getInstance();
        try {
            a.setTime(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        a.add(Calendar.DAY_OF_MONTH, num);
        return sdf.format(a.getTime());
    }

    /**
     * 判断是否为今天(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean IsToday(String day) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        Date date = getDateFormat().parse(day);
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    public static SimpleDateFormat getDateFormat() {
        if (null == DateLocal.get()) {
            DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
        }
        return DateLocal.get();
    }

    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();

}
